# gozerlib/web/event.py
#
#

""" web event. """

## gozerlib imports

from gozerlib.eventbase import EventBase
from gozerlib.utils.generic import splittxt

## basic imports

import cgi

class WebEvent(EventBase):

    def __init__(self): 
        EventBase.__init__(self)
        self.type = "web"

    def __deepcopy__(self, a):

        """ return a deepcopy """

        return WebEvent().copyin(self)

    def parse(self, response, request):

        """ parse request/response into a WebEvent. """

        input = request.get('content')
        self.isweb = True
        self.origtxt = input.strip()
        self.txt = self.origtxt
        self.cmnd = self.txt and self.txt.split()[0]
        self.groupchat = False
        self.response = response
        self.request = request
        self.makeargs()
        return self

    def _raw(self, txt, end=""):

        """ 
            put txt onto the reponse object .. adding end string if provided. 
            output is NOT escaped.

        """

        self.response.out.write(txt + end)

    def write(self, txt, end="\n"):

        """ 
            put txt onto the reponse object .. adding end string if provided. 
            output IS escaped.

        """

        self.response.out.write(cgi.escape(txt) + end)

    def reply(self, txt, resultlist=[], nritems=False, *args, **kwargs):

        """ send reply to the web user. """

        if self.checkqueues(resultlist):
            return

        result = self.makeresponse(txt, resultlist, nritems=False, *args, **kwargs)
        (res1, res2) = self.less(result)
        self.write(res1, '<br><br>')
        if res2:
            self.write(res2, '<br><br>')
